package com.holyshit.exam.net;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.holyshit.exam.Constants;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * RetrofitUtils
 * <p>
 * 提供Retrofit 常用的方法
 * <p>
 * Created by hero
 */
public class RetrofitUtils {
    private static final String TAG = "RetrofitUtils";
    private static RetrofitUtils instance = null;
    private final Retrofit retrofit;
    private final Retrofit RxRetrofit;
    private OkHttpClient okHttpClient;

    private RetrofitUtils() {

        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.url)
                .client(getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RxRetrofit = retrofit.newBuilder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }


    public static <T> T createApi(Class<T> clazz) {

        return getInstance().retrofit.create(clazz);
    }

    public static <T> T createRxApi(Class<T> clazz) {

        return getInstance()
                .RxRetrofit
                .create(clazz);

    }

    public static RetrofitUtils getInstance() {
        if (instance == null) {
            synchronized (RetrofitUtils.class) {
                if (instance == null) {
                    instance = new RetrofitUtils();
                }
            }

        }
        return instance;
    }

    public static RequestBody createBody(Map<String, Object> paramsMap) {
        if (paramsMap == null || paramsMap.size() == 0) {
            throw new IllegalArgumentException("不能创建空的RequestBody");
        }
        String s = JSON.toJSONString(paramsMap);
        return RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), s);
    }

    private static Gson getGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        return gsonBuilder.serializeNulls().create();
    }


    public OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            if (Constants.printLog) {
                HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
                httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                builder.addInterceptor(httpLoggingInterceptor);
                //builder.addNetworkInterceptor(new StethoInterceptor());//
            }
            builder.connectTimeout(5, TimeUnit.SECONDS);
            builder.readTimeout(10, TimeUnit.SECONDS);
            builder.writeTimeout(10, TimeUnit.SECONDS);
            return builder.build();
        }
        return okHttpClient;
    }
}
